package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Companies;
import com.example.springboot.entity.jobs;
import com.example.springboot.service.JobMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/JobManger")
public class JobMangerController {
    @Autowired
    private JobMangerService jobMangerService;

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<jobs> jobsList = jobMangerService.findAll();
        return Result.success(jobsList);
    }



    @GetMapping("/selectByTitle")
    public Result selectByTitle(@RequestParam String job_title) {
        List<jobs> jobsList = jobMangerService.selectByTitle(job_title);
        return Result.success(jobsList);
    }

    @GetMapping("/selectByCompany_id")
    public Result selectByCompany_id(@RequestParam int company_id) {
        List<Companies> companiesList = jobMangerService.selectByCompany_id(company_id);
        return Result.success(companiesList);
    }


    @PutMapping("/edit")
    public Result editUserByJob_id(@RequestBody jobs jobs) {
        jobMangerService.editUserByJob_id(jobs);
        return Result.success("编辑成功");
    }

    @DeleteMapping("/delete/{job_id}")
    public String deleteJob(@PathVariable int job_id) {
        jobMangerService.deleteJob(job_id);
        return "User record deleted successfully!";
    }
    @DeleteMapping("/deleteJobBatch")
    public Result deleteJobBatch(@RequestBody List<Long> ids) {
        try {
            jobMangerService.deleteJobBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
}
